home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et-2_2.lha / et2.2 / src / StreamConnection.C < prev    next >
C/C++ Source or Header  |  1990-12-06  |  2KB  |  89 lines

  1. //$StreamConnection$
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "StreamConnection.h"
  6. #include "Error.h"
  7.  
  8. //---- StreamConnection --------------------------------------------------------------
  9.  
  10. StreamConnection::StreamConnection(int f) : SysEvtHandler(f)
  11. {
  12.     ifp= ofp= 0;
  13.     Open(f);
  14. }
  15.  
  16. StreamConnection::~StreamConnection()
  17. {
  18.     if (ifp)
  19.     fclose(ifp);
  20.     if (ofp)
  21.     fclose(ofp);
  22. }
  23.     
  24. bool StreamConnection::Open(int fd)
  25. {
  26.     if (fd > 0) {
  27.     gSystem->AddFileInputHandler(this);
  28.     if (ifp= fdopen(fd, "r")) {
  29.         if (ofp= fdopen(fd, "w")) {
  30.         SetResourceId(fd);
  31.         return TRUE;
  32.         }
  33.     }
  34.     }
  35.     return FALSE;
  36. }
  37.  
  38. void StreamConnection::send(ServerMessages tag, byte *buf, u_long l)
  39. {
  40.     struct Header rp;
  41.  
  42.     rp.tag= tag;
  43.     rp.len= (u_int) l;
  44.     if (fwrite((char*)&rp, sizeof(struct Header), 1, ofp) != 1)
  45.     Error("send", "fwrite1");
  46.     if (buf && l > 0) {
  47.     if (fwrite((char*)buf, sizeof(char), (int) l, ofp) != l)
  48.         Error("send", "fwrite2");
  49.     }
  50.     fflush(ofp);
  51. }
  52.  
  53. ServerMessages StreamConnection::Receive(byte **buf, u_long *l)
  54. {
  55.     struct Header m;
  56.     m.tag= eMsgError;
  57.     m.len= 0;
  58.     
  59.     if (fread((char*)&m, sizeof(struct Header), 1, ifp) != 1) {
  60.     Error("Receive", "fread1");
  61.     Remove();
  62.     return eMsgError;
  63.     }
  64.     if (m.len > 0) {
  65.     SafeDelete(*buf);
  66.     *buf= new byte[m.len+1];
  67.     if (fread((char*)*buf, sizeof(char), m.len, ifp) != m.len) {
  68.         Error("Receive", "fread2");
  69.         Remove();
  70.         return eMsgError;
  71.     }
  72.     (*buf)[m.len]= 0;
  73.     *l= m.len;
  74.     }
  75.     return m.tag;
  76. }
  77.  
  78. void StreamConnection::Notify(SysEventCodes, int)
  79. {
  80.     byte *bp= 0;
  81.     u_long len= 0;
  82.     ServerMessages msg= Receive(&bp, &len);
  83.     Dispatch(msg, bp, len);
  84. }
  85.  
  86. void StreamConnection::Dispatch(ServerMessages, byte*, u_long)
  87. {
  88. }
  89.